using System;
using System.Collections.Generic;
using System.Xml;
using UnityEngine;

namespace EventBehaviorTree
{
    public static class NodeMsgBuilder
    {
        /// <summary>
        /// "NM/NM/NM/NM/NM/NM..."
        /// </summary>
        /// <param name="nodeMsgs"></param>
        /// <returns></returns>
        public static NodeMsg[] GEN_NodeMsgList(string nodeMsgs)
        {
            if (nodeMsgs == null) return new NodeMsg[0];
            string[] messages = XmlHelper.GetXmlStringList(nodeMsgs);
            NodeMsg[] nodeMessageList = new NodeMsg[messages.Length / 2 * 2];//avoid odd object
            for (int i = 0; i < nodeMessageList.Length; i++)
            {
                nodeMessageList[i] = (NodeMsg)Enum.Parse(typeof(NodeMsg), messages[i]);
            }
            return nodeMessageList;
        }
        public static NodeMsg[] GEN_NodeMsgList(XmlAttribute nodeMessages)
        {
            if (nodeMessages == null) return new NodeMsg[0];
            return GEN_NodeMsgList(nodeMessages.Value);
        }

        private static KeyValuePair<NodeMsg, NodeMsg>[] GEN_MsgPairs(params NodeMsg[] nodeMsgs)
        {
            int length = nodeMsgs.Length / 2;
            KeyValuePair<NodeMsg, NodeMsg>[] messagePairs = new KeyValuePair<NodeMsg, NodeMsg>[length];
            for (int i = 0; i < length; i++)
            {
                messagePairs[i] = new KeyValuePair<NodeMsg, NodeMsg>(nodeMsgs[i * 2], nodeMsgs[i * 2 + 1]);
            }
            return messagePairs;
        }
        public static KeyValuePair<NodeMsg, NodeMsg>[] GEN_MsgPairs(string nodeMsgs)
        {
            return GEN_MsgPairs(GEN_NodeMsgList(nodeMsgs));
        }
        public static KeyValuePair<NodeMsg, NodeMsg>[] GEN_MsgPairs(XmlAttribute nodeMsgs)
        {
            return GEN_MsgPairs(GEN_NodeMsgList(nodeMsgs));
        }
    }
}